unit VarSelect;

interface

uses
  Database, Vars,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, Menus, ComCtrls, ExtCtrls, Db, DBTables;

type
  TFormVarSelect = class(TForm)
    List1: TListBox;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    Label5: TLabel;
    List2: TListBox;
    Label1: TLabel;
    BtnAdd: TSpeedButton;
    BtnRemove: TSpeedButton;
    BtnUp: TSpeedButton;
    BtnDown: TSpeedButton;
    EditEvent: TEdit;
    Label2: TLabel;
    EditExample: TEdit;
    LabelExample: TLabel;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    Bevel1: TBevel;
    ImageB: TImage;
    Label4: TLabel;
    Bevel2: TBevel;
    ImageG: TImage;
    Label6: TLabel;
    BtnAddDummy: TSpeedButton;
    Bevel3: TBevel;
    Label7: TLabel;
    ImageY: TImage;
    Bevel4: TBevel;
    Label3: TLabel;
    ImageF: TImage;
    BtnHelpVars: TBitBtn;
    a1: TImage;
    a2: TImage;
    a3: TImage;
    a4: TImage;
    BtnVars: TBitBtn;
    Panel1: TPanel;
    Status: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnRemoveClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnUpClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure EditEventChange(Sender: TObject);
    procedure List1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure BtnAddDummyClick(Sender: TObject);
    procedure BtnVarsClick(Sender: TObject);
  private
    { Private declarations }
    procedure RefreshList1;
    procedure UpdateExample;
    function FigureBitmap(str: string): TBitmap;
    procedure RefreshVariables;
  public
    { Public declarations }
    vars: string;
  end;

var
  FormVarSelect: TFormVarSelect;
  olist: TStringList;
  stars: integer;

implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormVarSelect.FormCreate(Sender: TObject);
begin
  with ImageB.canvas do begin
    brush.color:=clbtnface;
    BrushCopy(Bounds(0,0,14,14),a1.picture.bitmap,
      Bounds(0,0,14,14),clsilver);
  end;
  with ImageG.canvas do begin
    brush.color:=clbtnface;
    BrushCopy(Bounds(0,0,14,14),a2.picture.bitmap,
      Bounds(0,0,14,14),clsilver);
  end;
  with ImageF.canvas do begin
    brush.color:=clbtnface;
    BrushCopy(Bounds(0,0,14,14),a3.picture.bitmap,
      Bounds(0,0,14,14),clsilver);
  end;
  with ImageY.canvas do begin
    brush.color:=clbtnface;
    BrushCopy(Bounds(0,0,14,14),a4.picture.bitmap,
      Bounds(0,0,14,14),clsilver);
  end;
  stars:=99999;
  RefreshVariables;
end;

procedure TFormVarSelect.FormDestroy(Sender: TObject);
begin
  olist.free;
end;

procedure TFormVarSelect.FormShow(Sender: TObject);
begin
  UpdateExample;
end;

procedure TFormVarSelect.BtnOKClick(Sender: TObject);
var i: integer;
begin
  if List2.items.count > 0 then begin
    if List2.items[0] = '<dummy variable>' then vars:='<@>'
    else vars:=List2.items[0];
    for i:=1 to List2.items.count-1 do begin
      if List2.items[i] = '<dummy variable>' then vars:=vars+',<@>'
      else vars:=vars+','+List2.items[i];
    end;
  end;
  close;
end;

procedure TFormVarSelect.BtnCancelClick(Sender: TObject);
begin
  vars:='!XxX!';
  close;
end;

procedure TFormVarSelect.BtnAddClick(Sender: TObject);
begin
  if (stars <> 99999) and (List2.items.count >= stars) then begin
    messagedlg('You must remove a variable before adding another',
      mtinformation,[mbok],0);
    exit;
  end;
  if List1.itemindex < 0 then exit;
  List2.items.add(List1.items[List1.itemindex]);
  List1.items.delete(List1.itemindex);
  UpdateExample;
end;

procedure TFormVarSelect.BtnRemoveClick(Sender: TObject);
begin
  if List2.itemindex < 0 then exit;
  List2.items.delete(List2.itemindex);
  RefreshList1;
  UpdateExample;
end;

procedure TFormVarSelect.RefreshList1;
var i, j: integer;
begin
  List1.items.assign(olist);
  if List2.items.count < 1 then exit;
  for i:=0 to List2.items.count-1 do begin
    j:=List1.items.indexof(List2.items[i]);
    if (j >= 0) and (List1.items[j] = List2.items[i]) then List1.items.delete(j);
  end;
end;

procedure TFormVarSelect.BtnUpClick(Sender: TObject);
var
  arg, i: integer;
  h: string;
begin
  arg:=(sender as TComponent).tag;
  i:=List2.itemindex;
  if i < 0 then exit;
  if (i = 0) and (arg < 0) then exit;
  if (i = (List2.items.count-1)) and (arg > 0) then exit;
  h:=List2.items[i+arg];
  List2.items[i+arg]:=List2.items[i];
  List2.items[i]:=h;
  List2.itemindex:=i+arg;
  UpdateExample;
end;

procedure TFormVarSelect.FormMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormVarSelect.BtnHelpClick(Sender: TObject);
begin
  if TComponent(sender).tag = 1 then Datawell.Help('processing_variables')
  else Datawell.Help('select_variables');
end;

procedure TFormVarSelect.UpdateExample;
var
  i, p: integer;
  work: string;
begin
  work:=EditEvent.text;
  for i:=0 to List2.items.count-1 do begin
    p:=pos('*',work);
    if p > 0 then begin
      if List2.items[i] = '<dummy variable>' then
        work:=copy(work,1,p-1)+'<@>'+copy(work,p+1,99999)
      else work:=copy(work,1,p-1)+'@'+List2.items[i]+copy(work,p+1,99999);
    end;
  end;
  EditExample.text:=work;
  if (stars <> 99999) and (List2.items.count >= stars) then begin
    List1.color:=clsilver;
    List1.enabled:=false;
    List1.tabstop:=false;
    BtnAdd.enabled:=false;
    BtnAddDummy.enabled:=false;
    LabelExample.caption:='Example of Variable Population by the Event (Completed)';
  end
  else begin
    List1.color:=clwindow;
    List1.enabled:=true;
    List1.tabstop:=true;
    BtnAdd.enabled:=true;
    BtnAddDummy.enabled:=true;
    LabelExample.caption:='Example of Variable Population by the Event';
  end;
end;

procedure TFormVarSelect.EditEventChange(Sender: TObject);
var
  work: string;
  i: integer;
begin
  work:=EditEvent.text;
  stars:=0;
  i:=pos('*',work);
  while i > 0 do begin
    inc(stars);
    delete(work,i,1);
    i:=pos('*',work);
  end;
end;

function TFormVarSelect.FigureBitmap(str: string): TBitmap;
begin
  if str = 'HP' then result:=a2.picture.bitmap
  else if str = 'MAXHP' then result:=a2.picture.bitmap
  else if str = 'SP' then result:=a2.picture.bitmap
  else if str = 'MAXSP' then result:=a2.picture.bitmap
  else if str = 'GP1' then result:=a2.picture.bitmap
  else if str = 'MAXGP1' then result:=a2.picture.bitmap
  else if str = 'GP2' then result:=a2.picture.bitmap
  else if str = 'MAXGP2' then result:=a2.picture.bitmap
  else if str = 'ENEMY' then result:=a2.picture.bitmap
  else if str = 'ENAME' then result:=a2.picture.bitmap
  else if str = 'GLINE1' then result:=a2.picture.bitmap
  else if str = 'GLINE2' then result:=a2.picture.bitmap
  else if str = 'AA' then result:=a3.picture.bitmap
  else if str = 'BB' then result:=a3.picture.bitmap
  else if str = 'CC' then result:=a3.picture.bitmap
  else if str = 'DD' then result:=a3.picture.bitmap
  else if str = 'EE' then result:=a3.picture.bitmap
  else if str = 'FF' then result:=a3.picture.bitmap
  else if str = '<dummy variable>' then result:=a4.picture.bitmap
  else result:=a1.picture.bitmap;
end;

procedure TFormVarSelect.List1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var bitmap: TBitmap;
begin
  with (control as TListBox).canvas do	begin
    FillRect(Rect);
    bitmap:=FigureBitmap(TListBox(control).items[index]);
    if Bitmap <> nil then begin
      BrushCopy(Bounds(Rect.left+2,Rect.top,bitmap.width,bitmap.height),bitmap,
        Bounds(0,0,Bitmap.width,Bitmap.height),clsilver);
    end;
    TextOut(Rect.left+16,Rect.top,(control as TListBox).items[Index])
  end;
end;

procedure TFormVarSelect.BtnAddDummyClick(Sender: TObject);
begin
  if (stars <> 99999) and (List2.items.count >= stars) then begin
    messagedlg('You must remove a variable before adding another',
      mtinformation,[mbok],0);
    exit;
  end;
  List2.items.add('<dummy variable>');
  UpdateExample;
end;

procedure TFormVarSelect.BtnVarsClick(Sender: TObject);
var form: TFormVars;
begin
  form:=TFormVars.create(self);
  form.nodelete:=true;
  form.BtnDelete.enabled:=false;
  form.showmodal;
  form.free;
  RefreshVariables;
end;

procedure TFormVarSelect.RefreshVariables;
var
  i: integer;
  table: TClientDataset;
begin
  List1.items.clear;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableVariables, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      List1.items.add(fieldbyname('FVariable').asstring);
      next;
    end;
  end;
  table.free;
  for i:=0 to Datawell.rvars.count-1 do List1.items.add(Datawell.rvars[i]);
  vars:='';
  olist:=TStringList.create;
  olist.assign(List1.items);
  RefreshList1;
  UpdateExample;
end;

end.




